package com.inforich.app.api.service.impl;

import com.inforich.app.api.dto.ProjectRubbishReportDTO;
import com.inforich.app.api.service.IAppProjectRubbishReportService;
import com.inforich.app.api.service.IAppProjectService;
import com.inforich.app.component.LoginUserApp;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.ApproveEnum;
import com.inforich.projectManage.domain.Project;
import com.inforich.projectManage.domain.ProjectRubbishReport;
import com.inforich.projectManage.service.IProjectRubbishReportService;
import java.util.Objects;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: some desc
 * @author: HJM
 * @date: 2022/10/29
 */
@Service
public class AppProjectRubbishReportServiceImpl implements IAppProjectRubbishReportService {

  @Autowired
  private IAppProjectService appProjectService;
  @Autowired
  private IProjectRubbishReportService projectRubbishReportService;

  @Override
  public AjaxResult addProjectReport(LoginUserApp loginUserApp, ProjectRubbishReportDTO dto) {
    int count = projectRubbishReportService.countByProjectId(dto.getProjectId());
    if (count > 0) {
      return AjaxResult.error("不可重复添加固废申报");
    }
    // 垃圾预估量不应大于处置重量
    if (dto.getDisposalPlanWeight().compareTo(dto.getDisposalWeight()) == 1) {
      return AjaxResult.error("垃圾预估量不应大于处置重量");
    }
    ProjectRubbishReport prp = new ProjectRubbishReport();
    BeanUtils.copyProperties(dto, prp);
    if ("1".equals(dto.getIsSubmit())) {
      prp.setApprovalStatus(ApproveEnum.REVIEW.getDbData());
    }
    return AjaxResult.success(projectRubbishReportService.save(prp));
  }

  @Override
  public AjaxResult updateProjectReport(LoginUserApp loginUserApp, ProjectRubbishReportDTO dto) {
    String id = dto.getId();
    ProjectRubbishReport prp = projectRubbishReportService.getById(id);
    if (Objects.equals(ApproveEnum.REVIEW.getDbData(), prp.getApprovalStatus())) {
      return AjaxResult.error("待审核的申报不可修改");
    }
    if (Objects.equals(ApproveEnum.PASS.getDbData(), prp.getApprovalStatus())) {
      return AjaxResult.error("已通过的申报不可修改");
    }
    BeanUtils.copyProperties(dto, prp);
    if ("1".equals(dto.getIsSubmit())) {
      prp.setApprovalStatus(ApproveEnum.REVIEW.getDbData());
    }
    return AjaxResult.success(projectRubbishReportService.updateById(prp));
  }

  @Override
  public AjaxResult myProjectReport(LoginUserApp loginUserApp) {
    Project project = appProjectService.getMyProject(loginUserApp);
    if (project == null) {
      return AjaxResult.error("请先进行项目认证");
    }
    ProjectRubbishReport rubbishReport = projectRubbishReportService.getProjectRubbishReportByProjectId(
        project.getId());
    if (rubbishReport == null) {
      rubbishReport = new ProjectRubbishReport();
      rubbishReport.setProjectId(project.getId());
      rubbishReport.setProjectName(project.getName());
      rubbishReport.setProjectAddress(project.getAddress());
      rubbishReport.setProjectKindId(project.getProjectKindId());
      rubbishReport.setProjectLocation(project.getLocation());
    }
    return AjaxResult.success(rubbishReport);
  }
}
